﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ObligatorioBD.MerObjects
{
    public class Atribute : IObjectId, IAtributeObject 
    {
        public enum AtributeType
        {
            StringType,
            IntType,
            DoubleType,
            BoolType,
            CompositeType
        }
        int id = -1;
        
        String name;
        AtributeType type;
        bool isPrimaryKey;
        int cantidad;
        List<Atribute> atributes;
        public Atribute(String name, AtributeType type, int quantity, bool isPrimaryKey = false)
        {
            this.name = name;
            this.type = type;
            this.isPrimaryKey = isPrimaryKey;
            this.cantidad = quantity;
            this.atributes = new List<Atribute>();
        }
        public static AtributeType GetType(string type)
        {
            AtributeType t = AtributeType.StringType;
            switch (type.ToUpper())
            {
                case "STRINGTYPE":
                    t = AtributeType.StringType;
                    break;
                case "DOUBLETYPE":
                    t = AtributeType.DoubleType;
                    break;
                case "BOOLTYPE":
                    t = AtributeType.BoolType;
                    break;
                case "INTTYPE":
                    t = AtributeType.IntType;
                    break;
                case "COMPOSITETYPE":
                    t = AtributeType.CompositeType;
                    break;
            }
            return t;
        }
        public String Name
        {
            get { return this.name; }
        }
        public AtributeType Type
        {
            get { return this.type; }
        }
        public bool IsPrimaryKey
        {
            get { return this.isPrimaryKey; }
            set { this.isPrimaryKey = value; }
        }
        public int ObjectId
        {
            get { return this.id; }
            set { this.id = value; }
        }
        public int Cantidad
        {
            get { return this.cantidad; }
            set { this.cantidad = value; }
        }
        public void AddAtribute(Atribute atribute)
        {
            this.atributes.Add(atribute);
        }
        public void RemoveAtribute(Atribute atribute)
        {
            this.atributes.Remove(atribute);
        }
        public List<Atribute> GetAtributes()
        {
            return this.atributes;
        }
        public Boolean HasAtributes() 
        {
            return this.atributes.Count > 0;
        }
        public override string ToString()
        {
            string atStr = this.Name + " (" + GetEnumString(this.type) + ")";
            if (this.Cantidad > 1)
            {
                atStr = atStr + " [1.." + this.Cantidad + "]";
            }
            return atStr;
        }
        private string GetEnumString(AtributeType type)
        {
            string t = "";
            switch (type.ToString().ToUpper())
            {
                case "STRINGTYPE":
                    t = "String";
                    break;
                case "DOUBLETYPE":
                    t = "Double";
                    break;
                case "BOOLTYPE":
                    t = "Boolean";
                    break;
                case "INTTYPE":
                    t = "Integer";
                    break;
                case "COMPOSITETYPE":
                    t = "Compuesto";
                    break;
            }
            return t;
        }
    }
}
